package eMdP.inputHandler.accions.EMdPHandler;

	/*
	 * Copyright (c) 2003-2009 jMonkeyEngine
	 * All rights reserved.
	 *
	 * Redistribution and use in source and binary forms, with or without
	 * modification, are permitted provided that the following conditions are
	 * met:
	 *
	 * * Redistributions of source code must retain the above copyright
	 *   notice, this list of conditions and the following disclaimer.
	 *
	 * * Redistributions in binary form must reproduce the above copyright
	 *   notice, this list of conditions and the following disclaimer in the
	 *   documentation and/or other materials provided with the distribution.
	 *
	 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 
	 *   may be used to endorse or promote products derived from this software 
	 *   without specific prior written permission.
	 *
	 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
	 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
	 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
	 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
	 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
	 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
	 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
	 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
	 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
	 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
	 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
	 */

	

	import java.util.logging.Logger;

	import com.jme.input.action.InputActionEvent;
	import com.jme.input.action.KeyInputAction;
import com.jme.math.FastMath;
	import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;
import com.jme.scene.shape.Sphere;
	import com.jme.util.Timer;
import com.jmetest.physics.Utils;
import com.jmex.bui.BLabel;
import com.jmex.physics.DynamicPhysicsNode;
import com.jmex.physics.PhysicsSpace;
import com.jmex.physics.material.Material;
import com.sun.net.ssl.internal.www.protocol.https.Handler;

import eMdP.classesBasiques.Paquet;
import eMdP.classesBasiques.Personatge;
import eMdP.estats.Partida;
import eMdP.gestors.GestorMusica;
import eMdP.gestors.GestorPantalla;
import eMdP.inputHandler.EMdPHandler;

	/**
	 * <code>KeyNodeLlansarPaquetAction</code> Llança el paquet de sobre de tot. Llança el paquet 
	 * en la direcció que mira el Personatge jugador.
	 * However, to prevent rolling, setting the lock axis to the world's y-axis (or
	 * any desired axis for that matter), will cause the node to rotate about the
	 * world. The locking of the axis is particularly useful for control schemes
	 * similar to first person shooters.
	 * 
	 * @author Mark Powell
	 * @version $Id: KeyNodeRotateLeftAction.java,v 1.12 2004/08/22 02:00:34 cep21
	 *          Exp $
	 */
	public class DispararEMdPHandler extends KeyInputAction {
		
		private static final Logger logger = Logger.getLogger(Partida.class.getName());
	    //el jugador que conté els paquets a llançar
	    private Personatge jugador;
	    private EMdPHandler handler;
	    private PhysicsSpace espaiFisic;


	    /**
	     * Constructor instancia un nou <code>KeyNodeLlansarPaquetAction</code>
	     * objecte utilitzant el parametre Personatge jugador.
	     * 
	     * @param jugador
	     *            El jugador que conté els paquets a llançar.
	     */
	    public DispararEMdPHandler(Personatge jugador, EMdPHandler handler, PhysicsSpace espaiFisic) {
	        this.jugador = jugador;
	        this.handler = handler;
	        this.espaiFisic = espaiFisic;
	    }
	    
	    /**
	     * <code>performAction</code> Llança l'últim paquet de la llista del jugador.
	     * 
	     * @see com.jme.input.action.KeyInputAction#performAction(InputActionEvent)
	     */
	    public void performAction(InputActionEvent evt) {
	    	//Si el personatge està bloquejat no el gira
	    	if(!jugador.estaTirant())
		    	if(!this.jugador.estaBloquejat(Timer.getTimer().getTimeInSeconds())){
		    		Vector3f direccioJugador = new Vector3f(this.jugador.capOnMira());
		    		Vector3f posJugador = new Vector3f(((Node)this.jugador.getChild("Fingers_R")).getWorldTranslation());
			    	// Comprovem que el jugador tingui algun paquet
			    	if(this.jugador.getNumPaquets() > 0){
			    		jugador.posaTirant();
			    		//Reproduim el crit
			    		//GestorMusica.reproduirCrit(this.jugador.getIdPersonatge()+1,this.jugador.getWorldTranslation());
			    		Paquet p = this.jugador.retornarEliminarPaquet();
			    		p.setLocalScale(p.getLocalScale().mult(this.jugador.getLocalScale()));
			    		//p.getNodeFisic().setMass(200f);
			    		p.getNodeFisic().setAffectedByGravity(true);
			    		this.jugador.getParent().attachChild(p);
			    		
			    		p.setActive(true);
			    		p.setHaEstatLlançat(true);
			    		p.clearDynamics();
			    		p.getLocalTranslation().set(posJugador.x+direccioJugador.x*8,9,posJugador.z+direccioJugador.z*8);
			    		
			    		Vector3f forsa = new Vector3f(direccioJugador.normalizeLocal());
			    		forsa.multLocal(8000);
			    		forsa.setY(1000);
			    		p.addForce(forsa);
			    		//try{Thread.sleep(300);}catch(Exception e){}
			    		
			    		((BLabel)GestorPantalla.getLlista().getLast().getFinestra().getComponent(4*jugador.getIdJugador()+4)).setText(""+jugador.getNumPaquets());
			    	}
		    	}	      
	    }
	}